package com.lzp.test;

import com.sun.deploy.net.HttpResponse;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

/**
 * @author : lzp
 * @version 1.0
 * @date : 2021/12/19 16:21
 * @apiNote : TODO
 */
@RestController
public class downLoadFile {
    @GetMapping("/getFile")
    public void getFile(HttpServletResponse response) throws IOException {
        System.out.println("接口被调用");
        File f =new File("D:\\12334.pdf");
        response.addHeader("Content-type",MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(f.getName().getBytes()));
        response.addHeader("Content-Length", "" + f.length());
        FileInputStream is=new FileInputStream(f);
        byte[] b = new byte[1024];
        int len;
        ServletOutputStream os = response.getOutputStream();
        while ((len=is.read(b))!=-1){
            os.write(b, 0, len);
        }
        os.close();
        is.close();
        System.out.println("传输完成");
    }

    @GetMapping("/sync")
    public void sync(HttpResponse response ) throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        HttpEntity<String> httpEntity = new HttpEntity<>("",headers);
        ResponseEntity<byte[]> exchange = restTemplate.exchange("http://localhost:8080/getFile", HttpMethod.GET, httpEntity, byte[].class);
        byte[] body = exchange.getBody();
        if (body==null){
            System.out.println("is==null");
            return;
        }
        System.out.println(Arrays.toString(body));
        InputStream is=new ByteArrayInputStream(body);
        FileOutputStream os = new FileOutputStream("D:\\同步文件.pdf");
        byte[] b = new byte[1024];
        int len;
        while ((len=is.read(b))!=-1){
            os.write(b, 0, len);
        }
        os.close();
        is.close();
        System.out.println("保存完成");
    }
    @GetMapping("/SyncBigFile")
    public void  SyncBigFile(){
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/getFile";
        String path = "D:\\同步文件1.pdf";
        RequestCallback requestCallback=request -> request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM,MediaType.ALL));
        restTemplate.execute(url,HttpMethod.GET,requestCallback, clientHttpResponse ->{
            Files.copy(clientHttpResponse.getBody(), Paths.get(path));
            return null;
        });

    }
}
